package com.zrx.controller;


import com.zrx.domain.ResponseResult;

import com.zrx.domain.dto.ChangeStatusDto;
import com.zrx.domain.entity.Role;
import com.zrx.domain.entity.RoleMenu;
import com.zrx.domain.vo.PageVo;
import com.zrx.service.RoleMenuService;
import com.zrx.service.RoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/system/role")
public class RoleController {

    @Autowired
    private RoleService roleService;




    /**
     * 角色分页查询
     * @param role
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public ResponseResult<PageVo> list(Role role, Integer pageNum, Integer pageSize) {
        return roleService.selectRolePage(role,pageNum,pageSize);
    }

    /**
     * 更改状态
     * @param statusDto
     * @return
     */
    @PutMapping("/changeStatus")
    public ResponseResult changeStatus(@RequestBody ChangeStatusDto statusDto){
        Role role = new Role();
        role.setId(statusDto.getId());
        role.setStatus(statusDto.getStatus());
        return ResponseResult.okResult(roleService.updateById(role));
    }
    /**
     * 新增角色
     */
    @PostMapping()
    public ResponseResult add( @RequestBody Role role)
    {
        roleService.insertRole(role);
        return ResponseResult.okResult();

    }

    /**
     * 根据id 获取角色信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult<PageVo> getByIdOneRole(@PathVariable("id") Long id) {

        return ResponseResult.okResult(roleService.getById(id));
    }



    /**
     * 修改保存角色
     */
    @PutMapping
    public ResponseResult edit(@RequestBody Role role)
    {
        roleService.updateRole(role);
        return ResponseResult.okResult();
    }

    /**
     * 删除角色
     * @param ids
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseResult deleteTag(@PathVariable("id") List<Long> ids){
        roleService.removeByIds(ids);
        return ResponseResult.okResult();
    }

    /**
     *查询全部角色信息
     * @return
     */
    @GetMapping("/listAllRole")
    public ResponseResult listAllRole(){
        List<Role> roles = roleService.selectRoleAll();
        return ResponseResult.okResult(roles);
    }
}
